package egs.comms;

import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;

import javax.net.ssl.SSLSocket;

import egs.packets.AuthLoginPacket;
import egs.packets.DisconnectPacket;
import egs.packets.Packet;
import egs.packets.PacketException;
import egs.packets.UseSecurePacket;
import egs.packets.UseSecureResponsePacket;
import egs.server.EGSServer;

public class TestDriver {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, IOException {
		ClientConnection conn = new ClientConnection(1024);
		conn.connect("localhost", 13337);
		System.out.println("Client connected to server");
		
		short[][] game_list = new short[2][2];
		game_list[0][0] = Packet.GAME_NAME_CHECKERS;
		game_list[0][1] = 1;
		game_list[1][0] = Packet.GAME_NAME_BLACKJACK;
		game_list[1][1] = 1;
		try {
			UseSecurePacket sec_p = new UseSecurePacket(UseSecurePacket.SecurityCode.tls);
			conn.sendPacket(sec_p);
			System.out.println("Data Sent");
			UseSecureResponsePacket secPacket = (UseSecureResponsePacket) conn.getPacket();
			System.out.println("received packet");
			short securePort = secPacket.getPort();
			System.out.println("Received secure port to use: " + securePort);
			conn.closeConnection();
			conn.secureConnect("localhost", securePort);
			System.out.println("opened secure connection");
			egs.packets.AuthLoginPacket al_p = 
				   new egs.packets.AuthLoginPacket( "jwalsh",
						    "foobar",
								    game_list );
			conn.sendPacket(al_p);
			System.out.println("Login packet sent");
			Packet receivedPacket = conn.getPacket();
			DisconnectPacket dp = new DisconnectPacket(DisconnectPacket.ReasonCode.client_quit);
			conn.sendPacket(dp);
			conn.closeConnection();
		} catch (PacketException e) {
			System.out.println("Could not create packet to send: " + e);
			e.printStackTrace();
		}
	 
	}

}
